<?php

/*
 * Copyright (C) 2013 Dimitrios I. Petridis, M.Sc, ICT & Software Engineer <dimi.petridis at gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Description of Nuke Oscillator
 * 
 * Prerequisites:
 * 1) osCommerce 2.2 Milestone 2 already set up http://www.oscommerce.com/
 * 2) phpnuke 7.x already set up https://www.phpnuke.org/
 * 3) oscommerce and php-nuke use different databases
 * 4) php 4.x
 * 5) mysql 4.x
 * 
 * WorkFlow:
 * This script is a php-nuke block that connects to oscommerces' database and fetches
 * data (i.e. products) that we need to present (only) in the center position of php-nuke.
 * This script stops the php-nuke database connection and then starts the oscommerce
 * database connection. After the script fetches the required data, the database connection 
 * to php-nuke is restored and the database connection to to oscommerce is stoped.
 * 
 * @author Dimitrios I. Petridis, M.Sc, ICT & Software Engineer <dimi.petridis at gmail.com>
 */

/* oscommerce settings */
$dbase_osc_host = "localhost";
$dbase_osc_username = "oscommerce_database_username";
$dbase_osc_password = "oscommerce_database_password";
$dbase_osc_name = "oscommerce_database_name";
$osc_path = "/path/to/oscommerce/catalog"; 

/* phpnuke settings */
$dbase_nuke_host = "localhost";
$dbase_nuke_username = "oscommerce_database_username";
$dbase_nuke_password = "oscommerce_database_password";
$dbase_nuke_name = "oscommerce_database_name";

/* ---- D O N ' T   E D I T   B E L O W    T H I S   L I N E ---- */


if (eregi("block-oscommerce.php", $_SERVER['PHP_SELF'])) {
    Header("Location: index.php");
    die();
}

global $bgcolor1, $bgcolor2, $bgcolor3;
$border = $bgcolor1;
$table_bgcolor = $bgcolor1;

@ $dbw = mysql_connect("$dbase_osc_host", "$dbase_osc_username", "$dbase_osc_password");
if (!$dbw) {
    echo "Error: Could not connect to database.  Please try again later.";
    exit;
}

mysql_select_db("$dbase_osc_name");

$query = "SELECT p.products_id, p.products_image, p.products_price, s.products_name, s.products_description FROM products p LEFT JOIN products_description s ON p.products_id = s.products_id WHERE products_status = '1' ORDER BY p.products_date_added DESC LIMIT 0 , 9";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);

for ($i = 0; $i < $num_results; $i++) {
    $j = 0;
    $row = mysql_fetch_array($result);

    $proion[$i][$j] = $row["products_id"];
    $j++;

    $proion[$i][$j] = stripslashes($row["products_image"]);
    $j++;

    $proion[$i][$j] = $row["products_price"];
    $j++;

    $proion[$i][$j] = $row["products_name"];
    $j++;

    $proion[$i][$j] = $row["products_description"];
    $j++;
}

$content .="<align=center><table width=\"100%\"  border=\"$border\" bordercolor=\"$bgcolor2\" bgcolor=\"$table_bgcolor\">";
$content .= "<tr><td width=\"100%\" height=\"28\" colspan=\"6\" align=\"center\" bgcolor=\"$bgcolor2\" $table_top ><b>Τελευταία Προίοντα απο το Ηλεκτρονικό Κατάστημα μας</b></td></tr>";
$content .="</table>";
$content .="<br>";
$content .="<align=center><table width=\"100%\" border=\"$border\" bordercolor=\"$bgcolor2\" bgcolor=\"$table_bgcolor\">";

$content .="<TR>";
$content .="<COLGROUP width=\"33%\" valign=\"top\" align=\"center\">";
$content .="<COLGROUP width=\"33%\" valign=\"top\" align=\"center\">";
$content .="<COLGROUP width=\"34%\" valign=\"top\" align=\"center\">";
$content .="<td>";

for ($i = 0; $i < $num_results; $i++) {
    if ($i == 3 || $i == 6 || $i == 9)
        $content .="<tr><td>";

    $id = $proion[$i][0];
    $image = $proion[$i][1];
    $price = intval($proion[$i][2]);
    $name = mb_convert_encoding($proion[$i][3], "iso-8859-7", "UTF-8");
    $desc = mb_convert_encoding($proion[$i][4], "iso-8859-7", "UTF-8");


    $picture = "<a href=\"$osc_path\product_info.php?products_id=$id\" target=\"_blank\"><img src=\"/commerce/catalog/images/$image\" Alt=\"$desc\" width=\"80\" height=\"80\"></a>";

    $content .= " $picture " . "<br>" . "<font color=\"$bgcolor2\"><b>A/A Προιόντος : </b></font>" . "$id" . "<br>" . "<font color=\"$bgcolor2\"><b>Όνομα Προιόντος : </b></font>" . "$name" . "<br>" . "<font color=\"$bgcolor2\"><b>Τιμή : </b></font>" . " $price " . "euro + 18% ΦΠΑ";
    $content .="<td>";
}
$content .="</table>";


mysql_close();
@ $db = mysql_pconnect("$dbase_nuke_host", "$dbase_nuke_username", " $dbase_nuke_password");
mysql_select_db("$dbase_nuke_name");
?>
